package com.onion.core.context;

import javax.annotation.PostConstruct;
import java.lang.management.ManagementFactory;

public class OnionContext {

    private static String appId;
    private static String appName;
    private static String appNode;
    private static String appUniqueId;
    private static Integer pId;

    @PostConstruct
    public void init(){
        //初始化配置信息
        OnionConfig.instance().init();

        //初始化应用信息
        appId = OnionConfig.get("system.app.id");
        appId = OnionConfig.get("system.app.name");
        appNode = OnionConfig.get("system.app.node");
        appUniqueId = appId+"_"+appNode;

        //初始化当前进程ID
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String curPid = name.split("@")[0];
        pId = Integer.parseInt(curPid);
    }

    /**
     * 获取应用标识
     * @return
     */
    public static String getAppId(){
        return appId;
    }

    /**
     * 获取应用名称
     * @return
     */
    public static String getAppName(){
        return appName;
    }

    /**
     * 获取应用节点
     * @return
     */
    public static String getAppNode(){
        return appNode;
    }

    /**
     * 获取进程ID
     * @return
     */
    public static long getPId(){
        return pId;
    }

    /**
     * 获取应用唯一标识【appId + appNode】
     * @return
     */
    public static String getAppUniqueId() {
        return appUniqueId;
    }

}